﻿using System.Collections.Generic;
using System.Linq;
using Basic4Net.Entities;

namespace Basic4Net.BusinessRules
{
    public class Post : BaseBR
    {
        private static Post _postInstance;
        public static Post PostInstance
        {
            get { return _postInstance ?? (_postInstance = new Post()); }
        }

        /// <summary>
        /// 获取职务信息  
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public List<t_Post> FindAll(string schoolMark)
        {
            using (DBEntities db = new DBEntities())
            {
                IEnumerable<t_Post> list =
                    from post in db.t_Post
                    where post.SchoolMark == schoolMark
                    select post;

                List<t_Post> newList = (List<t_Post>)list.ToList();
                return newList;
            }
        }

        public int FindCount(t_Post entity)
        {
            using (DBEntities db = new DBEntities())
            {
                int count = (from b in db.t_Post
                             where b.PostName == entity.PostName
                             select b).Count();
                return count;
            }
        }

  
        /// <summary>
        /// 添加新职称
        /// </summary>
        /// <param name="entity">职称实体</param>
        /// <returns>是否成功</returns>
        public bool Add(t_Post entity, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                //效验重名
                int CheckName = (from b in db.t_Post
                                 where b.PostName == entity.PostName
                                 select b).Count();
                if (CheckName > 0)
                {
                    msg = "已经存在名称为【" + entity.PostName + "】的职称.";
                    result = false;
                }

                if (result)
                {
                    db.AddTot_Post(entity);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        result = false;
                        msg = "操作失败！";
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// 修改车辆
        /// </summary>
        /// <param name="entity">车辆实体</param>
        /// <param name="msg">错误信息</param>
        /// <returns>是否成功</returns>
        public bool Update(t_Post entity, ref string msg)
        {
            bool result = true;

            using (DBEntities db = new DBEntities())
            {
                //效验重名
                int CheckName = (from b in db.t_Post
                                 where b.PostName == entity.PostName && b.RID != entity.RID
                                 select b).Count();
                if (CheckName > 0)
                {
                    msg = "已经存在名称为【" + entity.PostName + "】的职称.";
                    result = false;
                }

                if (result)
                {
                    var query =
                        from ord in db.t_Post
                        where ord.RID == entity.RID
                        select ord;

                    foreach (t_Post item in query)
                    {
                        item.PostName = entity.PostName;
                        item.ConvertNum = entity.ConvertNum;
                    }

                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        result = false;
                        msg = "操作失败！";
                    }
                    //db.t_Post.FirstOrDefault(p => p.RID == entity.RID);
                    //db.ApplyCurrentValues("t_Post", entity);
                    //try
                    //{
                    //    db.SaveChanges();
                    //}
                    //catch
                    //{
                    //    result = false;
                    //    msg = "操作失败！";
                    //}
                }
                return result;
            }
        }

        /// <summary>
        /// 修改车辆
        /// </summary>
        /// <param name="entity">车辆实体</param>
        /// <param name="msg">错误信息</param>
        /// <returns>是否成功</returns>
        public bool Delete(t_Post entity, ref string msg)
        {
            bool result = true;

            using (DBEntities db = new DBEntities())
            {
                var list = from post
                           in db.t_Post
                           where post.RID == entity.RID
                           select post;
                foreach (t_Post tempPost in list.ToList<t_Post>())
                {
                    try
                    {
                        var relalist = (from t in db.t_PostRelation
                                        where t.PostID == tempPost.RID
                                        select t);
                        foreach (t_PostRelation pr in relalist.ToList<t_PostRelation>())
                        {
                            db.DeleteObject(pr);
                            db.SaveChanges();
                        }

                        db.DeleteObject(tempPost);
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败！";
                        result = false;
                    }
                } 
                return result;
            }
        }
    }
}
